package com.tyqx.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class PrintDirUtil {
    public static List<File> fileList = new ArrayList<>();

    public PrintDirUtil(){
        fileList = new ArrayList<>();
    }

    private static final Logger logger = LoggerFactory.getLogger(PrintDirUtil.class);

    public static Integer count = 0;

    public static void filterFile(File fileterDir,String fileSuffix) {
        try {
            if(fileterDir.isDirectory()) {
                // 匿名内部类方式,创建过滤器子类对象
                File[] files = fileterDir.listFiles(new FileFilter() {
                    @Override
                    public boolean accept(File pathname) {
                        return pathname.getName().endsWith(fileSuffix) || pathname.isDirectory();
                    }
                });
                if (null != files && files.length > 0) {
                    // 循环打印
                    for (File file : files) {
                        System.out.println("正在检索的路径：" + file.getAbsolutePath());
                        if (file.isFile()) {
                            logger.info("文件名:" + file.getAbsolutePath() + ",文件大小：" + file.length());
                            fileList.add(file);
                        } else {
                            if(file.isDirectory()) {
                                filterFile(file, fileSuffix);
                            }
                        }
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    //查找两个集合内不相等的元素
    public static <T> List<T> compare(List<T> t1, List<T> t2) {
        List<T> list1 = t1;
        List<T> list2 = new ArrayList<T>();
        for (T t : t2) {
            if (!list1.contains(t)) {
                list2.add(t);
            }
        }
        return list2;
    }



    /**
     * 获取属性值
     * @param fieldName 字段名称
     * @param o 对象
     * @return  Object
     */
    private static Object getFieldValueByName(String fieldName, Object o) {
        try {
            String firstLetter = fieldName.substring(0, 1).toUpperCase();
            String getter = "get" + firstLetter + fieldName.substring(1);    //获取方法名
            Method method = o.getClass().getMethod(getter, new Class[] {});  //获取方法对象
            Object value = method.invoke(o, new Object[] {});    //用invoke调用此对象的get字段方法
            return value;  //返回值
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
